/** 定义新闻相关接口 */
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const Response = require("../utils/Response");

// 引入mysql连接池
const pool = require("../utils/pool");

/** 新增特色景点 */
router.post("/scenic-spot/add", (req, res) => {
  let { cover, descrip, title,type,cid  } = req.body;
  let schema = Joi.object({
    cover: Joi.string(),
    descrip: Joi.string().required(),
    title: Joi.string().required(),
    type: Joi.string().required(),
    cid: Joi.number().required()
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    console.log(error);
    res.send(Response.error(400, error));
    return;
  }
  let obj = {
    cover,
    descrip,
    title,
    type,
    cid,
  };
  const sql = "insert into sight set ?";
  pool.query(sql, [obj], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
  });
});

/** 修改特色景点 */
router.post("/scenic-spot/update", (req, res) => {
  let { id, cover, descrip, title,type,cid } = req.body;
  let schema = Joi.object({
    id: Joi.number().required(),
    cover: Joi.string(),
    descrip: Joi.string().required(),
    title: Joi.string().required(),
    type: Joi.string().required(),
    cid: Joi.number().required()
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  // 表单验证通过，执行添加操作
  let sql = "update sight set cover=?,descrip=?,title=?,type=?,cid=? where id =?";
  pool.query(sql, [cover, descrip, title, type, cid, id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
    console.log(result);
  });
});

/** 更换图片 */
router.post("/scenic-spot/pic", (req, res) => {
  let { id, cover } = req.body;
  let schema = Joi.object({
    id: Joi.number().required(),
    cover: Joi.string(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  // 表单验证通过，执行添加操作
  let sql = "update sight set cover=? where id =?";
  pool.query(sql, [cover, id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
    console.log(result);
  });
});

/** 修改类型 */
router.post("/scenic-spot/types", (req, res) => {
  let { id,type,cid } = req.body;
  let schema = Joi.object({
    id: Joi.number().required(),
    type:Joi.string(),
    cid:Joi.number()
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  // 表单验证通过，执行添加操作
  let sql = "update sight set type=?,cid=? where id =?";
  pool.query(sql, [type,cid, id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
    console.log(result);
  });
});

/** 删除特色景点 */
router.post("/scenic-spot/delete", (req, res) => {
  let { id } = req.body;
  let schema = Joi.object({
    id: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = "delete from sight where id = ?";
  pool.query(sql, id, (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
    console.log(result);
  });
});

/** 分页查询特色景点 */
router.get("/scenic-spot/search",async (req, res) => {
  let { page, pagesize } = req.query;
  let schema = Joi.object({
    page: Joi.number().required(),
    pagesize: Joi.number().integer().required(),
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    console.log(error);
    res.send(Response.error(400, error));
    return;
  }

// 执行查询数组业务
try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from sight limit ?,?";
    let result = await pool.querySync(sql, [startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from sight";
    let result2 = await pool.querySync(sql2, [startIndex, size]);
    let total = result2[0].count;
    res.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    res.send(Response.error(error));
  }

});

/** 类别id查询特色景点(分页) */
router.get("/scenic-spot/category", async (req, res) => {
  // 获取请求参数   get请求的参数封装在req.query中
  let { page, pagesize, cid } = req.query;

  //TODO 服务端表单验证  如果验证通过那么继续后续业务  如果验证不通过，则直接返回参数异常
  let schema = Joi.object({
    cid: Joi.number().required(), // cid必须是数字，必填
    page: Joi.number().required(), // page必须是数字，必填
    pagesize: Joi.number().integer().required(), // pagesize必须是不大于100的数字，必填
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }

  // 执行查询数组业务
  try {
    cid = parseInt(cid);
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from sight where cid =? limit ?,?";
    let result = await pool.querySync(sql, [cid, startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from sight where cid=?";
    let result2 = await pool.querySync(sql2, [cid]);
    let total = result2[0].count;
    res.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    res.send(Response.error(error));
  }
});

/** 类别id查询特色景点 */
router.get("/scenic-spot/cid",(req,res)=>{
  let {cid} = req.query
  let schema = Joi.object({
    cid: Joi.number().required()
  })
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = 'select * from sight where cid =?'
  pool.query(sql,[cid],(error,result)=>{
    if(error){
      res.send(Response.error(500, error));
      throw error
    }
    res.send(Response.ok(result))
  })
})
// 将router对象导出
module.exports = router;
